package com.malou.client.ventas.cliente;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;

public class ClienteDialog extends Dialog {

	 protected HiddenField<String> id;
	 protected TextField<String> apellidos;
	 protected TextField<String> nombres;
	 protected TextField<String> direccion;
	 protected TextField<String> ruc;
	 protected TextField<String> telefono;
	 protected TextField<String> celular;
	 protected TextField<String> email;
	 protected TextField<String> razonsocial;
	 protected TextField<String> dni;
	 protected Button guardar;
	 protected Button cancelar;
	 protected Status status;
	 //public ClientePanel ClientePanel;
	 private FormBinding formBindings;
	 Cliente cl;
	 private MalouConstants constants = GWT.create(MalouConstants.class);
	 
	 public ClienteDialog(){
	    setLayout(new FitLayout());
		setModal(true);
		setPlain(true);
		setSize(450, 320);
		setClosable(true);
		
		FormPanel panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    panel.setLabelWidth(110);
	    
	    formBindings = new FormBinding(panel, false);	    
	    addFields(panel);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    add(panel);
	    
	    setBodyBorder(true);	    
	    setResizable(false);	    
	    setFocusWidget(nombres);
	 }
	 
	 @Override
	  protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.guardando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar());
	    
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  ClienteDialog.this.hide();
	      }

	    });

	    guardar = new Button(constants.guardar());
	    
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	  if(validate()){
	    		status.show();
   		    getButtonBar().disable();
   		    
   			cl = new Cliente();
   			//System.out.println(cl.getId());
   			if(id.getValue() != null)
   				cl.setId(Integer.valueOf(id.getValue()));
   			cl.setNombres(nombres.getValue());
   			cl.setApellidos(apellidos.getValue());
   			cl.setDireccion(direccion.getValue());
   			cl.setRuc(ruc.getValue());
   			cl.setTelefono(telefono.getValue());
   			cl.setCelular(celular.getValue());
   			cl.setEmail(email.getValue());
   			cl.setRazonsocial(razonsocial.getValue());
   			cl.setDni(dni.getValue());
   			
   			Clientes.clienteDialog = ClienteDialog.this;   			
   			Clientes.save(cl);	    			
	    	}	    		 
	    	  
	      }
	    });
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	  }
	 protected boolean validate() {
		boolean ape = apellidos.isValid();
		boolean nom = nombres.isValid();
		boolean dir = direccion.isValid();
		boolean ru = ruc.isValid();
		boolean tel = telefono.isValid();
		boolean cel = celular.isValid();		
		boolean em = email.isValid();
		boolean rz = razonsocial.isValid();
		boolean dn = dni.isValid();
	    return ape && nom && dir && ru && tel && cel && em && rz && dn;
	  }
	 
	  public void reset(){
		  setIconStyle("icon-new");
		  setHeading(constants.clienteNuevoCliente());
		  		  
		  id.clear();
		  apellidos.clear();
		  nombres.clear();
		  direccion.clear();
		  ruc.clear();
		  telefono.clear();
		  celular.clear();
		  email.clear();
		  razonsocial.clear();
		  dni.clear();
		  status.hide();
		  getButtonBar().enable();
	  }
	 
	  public void setStore(ListStore<BeanModel> store){
		  //fieldBinding(campo caja de texto,campo de la clase)
		   //id, apellidos, nombres, direccion, ruc, telefono, celular, email,
		    //razonsocial, dni
		  formBindings.addFieldBinding(new FieldBinding(id, "id"));
		  formBindings.addFieldBinding(new FieldBinding(apellidos, "apellidos"));
		  formBindings.addFieldBinding(new FieldBinding(nombres, "nombres"));
		  formBindings.addFieldBinding(new FieldBinding(direccion, "direccion"));
		  formBindings.addFieldBinding(new FieldBinding(ruc, "ruc"));
		  formBindings.addFieldBinding(new FieldBinding(telefono, "telefono"));
		  formBindings.addFieldBinding(new FieldBinding(celular, "celular"));
		  formBindings.addFieldBinding(new FieldBinding(email, "email"));
		  formBindings.addFieldBinding(new FieldBinding(razonsocial, "razonsocial"));
		  formBindings.addFieldBinding(new FieldBinding(dni, "dni"));
		  
		  formBindings.setStore(store);
	  }
	 
	 private void addFields(ContentPanel cp) {

			id = new HiddenField<String>();
			id.setVisible(false);
			cp.add(id);
			
			nombres = new TextField<String>();
		    nombres.setMinLength(3);
		    nombres.setFieldLabel(constants.clienteNombres());	    
		    nombres.setAllowBlank(false);	    
		    cp.add(nombres, new FormData("-20"));
		    
		    apellidos = new TextField<String>();
		    apellidos.setMinLength(4);
		    apellidos.setFieldLabel(constants.clienteApellidos());	    
		    apellidos.setAllowBlank(false);
		    cp.add(apellidos, new FormData("-20"));	       
		    
		    direccion = new TextField<String>();
		    direccion.setMinLength(3);		    
		    direccion.setFieldLabel(constants.clienteDireccion());	    
		    direccion.setAllowBlank(false);
		    cp.add(direccion, new FormData("-20"));
		    
		    ruc = new TextField<String>();
		    ruc.setMinLength(3);	    
		    ruc.setFieldLabel(constants.clienteRuc());	    
		    ruc.setAllowBlank(false);
		    ruc.setMaxLength(11);
		    ruc.setMinLength(11);
		    cp.add(ruc, new FormData("-20"));
		    	    
		    telefono = new TextField<String>();
		    telefono.setMinLength(3);	    
		    telefono.setFieldLabel(constants.clienteTelefono());	    
		    cp.add(telefono, new FormData("-20"));
		    
		    celular = new TextField<String>();
		    celular.setMinLength(3);	    
		    celular.setFieldLabel(constants.clienteCelular());	    
		    cp.add(celular, new FormData("-20"));
		    
		    email = new TextField<String>();
		    email.setMinLength(3);	    
		    email.setFieldLabel(constants.clienteEmail());
		    
		    final String emailReg = "[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
		    final String errMsg = constants.emailNoValido();
		    email.setValidator(new Validator() {
		      public String validate(Field<?> field, String value) {
		        if (!value.toUpperCase().matches(emailReg))
		          return errMsg;
		        return null;
		      }
		    });
		    
		    cp.add(email, new FormData("-20"));
		    
		    razonsocial = new TextField<String>();
		    razonsocial.setMinLength(3);
		    razonsocial.setFieldLabel(constants.clienteRazonSocial());
		    cp.add(razonsocial, new FormData("-20"));
		    
		    dni = new TextField<String>();
		    dni.setMinLength(3);
		    dni.setFieldLabel(constants.clienteDni());
		    dni.setMaxLength(8);
		    dni.setMinLength(8);
		    cp.add(dni, new FormData("-20"));
		    
	   }
	 
	  public void beforeEdit(){
		  setIconStyle("icon-update");
		  setHeading(constants.clienteModificarCliente());		  
		  
		  status.hide();
		  getButtonBar().enable();
	  }
	  public void bind(BeanModel selectedModel){		  
		  formBindings.bind(selectedModel);
	  }
	  public void unbind(){
		  formBindings.unbind();
	  }
}